<!DOCTYPE html>
<html lang="en" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/css/custom.form.css">
</head>
<body>
<div class="panel panel-default operation" hidden>
    <div class="panel-heading title"></div>
    <div class="layui-card-body" >
        <form class="layui-form " action="" lay-filter="info" style="width: 700px;margin-top: 10px" id="form1">
            <input name="id" hidden/>
            <div class="layui-form-item">
                <label class="layui-form-label">商品名</label>
                <div class="layui-input-block">
                    <input type="goodsName" name="goodsName" placeholder="请输入商品名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品数量</label>
                <div class="layui-input-block">
                    <input type="number"  name="goodsCount" placeholder="请输入商品数量" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">商品类别</label>
                <div class="layui-input-block">
                    <!--<input type="goodsCategory"  name="goodsCategory" placeholder="请输入商品类别" autocomplete="off"-->
                           <!--class="layui-input">-->
                    <div class="layui-inline" style="height: 38px">
                        <select name="goodsCategory" lay-verify="required" class="layui-bg-blue" style="height: 38px;">
                            <option value="" >请选择类别</option>
                            <option value="家具">家具</option>
                            <option value="电器">电器</option>
                            <option value="水果">水果</option>
                            <option value="饮品">饮品</option>
                            <option value="零食">零食</option>
                        </select>
                    </div>
                </div>
                <!--<select name="city" lay-verify="">-->
                    <!--<option value="">请选择一个类别</option>-->
                    <!--<option value="010">北京</option>-->
                    <!--<option value="021">上海</option>-->
                    <!--<option value="0571">杭州</option>-->
                <!--</select>-->
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">创建时间</label>
                <div class="layui-input-block">
                    <input type="createTime" name="createTime" placeholder="请输入创建时间" autocomplete="off" id="create_time"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">更新时间</label>
                <div class="layui-input-block">
                    <input type="updateTime" name="updateTime" placeholder="请输入更新时间" autocomplete="off" id="update_time"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">上架状态</label>
                <div class="layui-input-block">
                    <input type="goodsStatus" name="goodsStatus" placeholder="请输入是否上架(1 上架；0 下架)" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">

                <input name="imagePath" hidden/>
                <label class="layui-form-label">上传文件</label>
                <div class="layui-input-inline">
                    <button type="button" class="layui-btn" id="upload">
                        <i class="layui-icon">&#xe67c;</i>上传文件
                    </button>
                </div>
                <div class="upload-show">

                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit="" lay-filter="submit">保存</button>
                    <button class="layui-btn layui-btn-primary" id="btn_cancel">返回</button>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="table_div">
    <div id="searchParam" shiro:hasPermission="shop:list">
        <div class="layui-form-item">
            <div class="layui-input-inline">
                <input type="text" id="key" class="layui-input" autocomplete="off" placeholder="请输入">
            </div>

            <div class="layui-input-inline ">
                <button class="layui-btn" onclick="search()" id="search">查询</button>
            </div>
        </div>

    </div>
    <table class="layui-table" id="showTable" lay-filter="showTable"></table>
</div>
<script type="text/html" id="toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add" shiro:hasPermission="shop:add">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDeleted" shiro:hasPermission="shop:delete">删除</button>
    </div>
</script>



<script type="text/html" id="grounding">
    <button class="layui-btn layui-btn-xs" lay-event="pushOn">
        上架</button>
    <button class="layui-btn layui-btn-danger layui-btn-xs" lay-event="pullOn">
        <!--<i class="layui-icon layui-icon-download-circle"></i>-->
        下架</button>
    <!--<button class="layui-btn layui-btn-xs" lay-event="pushOn" >上架</button>-->
    <!--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="pullOn">下架</a>-->
</script>

<script type="text/html" id="tool">
    <a class="layui-btn layui-btn-xs" lay-event="edit" shiro:hasPermission="shop:update">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="shop:delete">删除</a>
</script>

</body>
</html>
<script src="/layui/layui.all.js"></script>
<script src="/js/core.util.js"></script>
<script>
    //获取token
    var token = CoreUtil.getData("access_token");
    //地址栏转义token中的#号
    var tokenQuery = token.replace("#", "%23");
    var tableIns1;
    var table = layui.table;
    var form = layui.form;
    var layer = layui.layer;
    var $ = jQuery = layui.jquery;
    var laydate = layui.laydate;
    var upload = layui.upload;
    layui.use(['table', 'layer', 'laydate','upload'], function () {
        //文件是上传执行实例
        var uploadInst = upload.render({
            elem: '#upload' //绑定元素
            ,url: '/upload/dianziqianming?authorization='+tokenQuery //上传接口
            ,field:'file'
            ,done: function(res){
                $(".operation input[name=" +"imagePath"+ "]").val(res.data);
                $(".upload-show").html("");
                $("<img>").attr("src","/uploadImage/"+res.data).appendTo( $(".upload-show")).width(300);

                /* search();
                 //上传完毕回调*/
            }
            ,error: function(){
                //请求异常回调
            }
        });


        //加载table
        tableIns1 = table.render({
            elem: '#showTable'
            , contentType: 'application/json'
            , headers: {"authorization": token}
            , page: true //开启分页
            , url: '/shop/listByPage' //数据接口
            , method: 'POST'
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": CoreUtil.isEmpty(res.data) ? 0 : res.data.total, //解析数据长度
                    "data": CoreUtil.isEmpty(res.data) ? null : res.data.records //解析数据列表
                }
            }
            , cols: [
                [
                    {type: 'checkbox', fixed: 'left'},
                    // {field: 'id', title: '主键', sort: true},
                    {field: 'goodsName', title: '商品名', sort: true},
                    {field: 'goodsCount', title: '商品数量', sort: true},
                    {field: 'goodsCategory', title: '商品类别', sort: true},
                    //{field: 'contentJson', title: '所有内容的JSON 字符串', sort: true},
                    {field: 'createTime', title: '创建时间', sort: true},
                    {field: 'updateTime', title: '更新时间', sort: true},
                    {field: 'goodsStatus', title: '上架状态', sort: true},
                    // {field: 'deleted', title: '是否删除(1 未删除；0 删除)', sort: true},
                    {width: 120, toolbar: "#grounding", title: '是否上下架'},
                    {width: 120, toolbar: "#tool", title: '操作'}
                ]
            ]
            , toolbar: '#toolbar'
        });


        //表头工具
        table.on('toolbar(showTable)', function (obj) {
            switch (obj.event) {
                case 'batchDeleted':
                    var checkStatus = table.checkStatus(obj.config.id);
                    var data = checkStatus.data;
                    if (data.length == 0) {
                        layer.msg("请选择要批量删除的列");
                    } else {
                        var ids = [];
                        $(data).each(function (index, item) {
                            ids.push(item.id);
                        });
                        tipDialog(ids);
                    }
                    break;
                case 'add':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("新增");
                    $("#form1")[0].reset();
                    laydate.render({
                        elem: '#create_time' //指定元素
                        ,type: 'datetime'
                        ,value: new Date()
                    });
                    laydate.render({
                        elem: '#update_time' //指定元素
                        ,type: 'datetime'
                        ,value: new Date()
                    });
                    // $(".operation input[name=id]").val("");
                    // $(".operation input[name=goodsName]").val("");
                    // $(".operation input[name=goodsCount]").val("");
                    // $(".operation input[name=goodsCategory]").val("");
                    // $(".operation input[name=contentJson]").val("");
                    // $(".operation input[name=createTime]").val("");
                    // $(".operation input[name=updateTime]").val("");
                    // $(".operation input[name=goodsStatus]").val("");
                    //$(".operation input[name=deleted]").val("");
                    break;

            }
            ;
        });
        //列操作
        table.on('tool(showTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'del':
                    var ids = [];
                    ids.push(data.id);
                    tipDialog(ids);
                    break;
                case 'edit':
                    $(".table_div").hide();
                    $(".operation").show();
                    $(".title").html("编辑");
                    // var parse = JSON.parse(data.contentJson);
                    // $.each(parse,function(e,t) {
                    //     $(".operation input[name="+t.key+"]").val(t["value"]);
                    // })
                     $(".operation input[name=id]").val(data.id);
                     $(".operation input[name=goodsName]").val(data.goodsName);
                     $(".operation input[name=goodsCount]").val(data.goodsCount);
                     $(".operation input[name=goodsCategory]").val(data.goodsCategory);
                    // $(".operation input[name=contentJson]").val(data.contentJson);
                    $(".operation input[name=createTime]").val(data.createTime);
                     $(".operation input[name=updateTime]").val(data.updateTime);
                     $(".operation input[name=goodsStatus]").val(data.goodsStatus);
                    // $(".operation input[name=deleted]").val(data.deleted);
                    break;

                case 'pushOn' :

                     CoreUtil.sendPost("/on/pushOn/"+data.id,null, function (){
                        location.reload();
                     });
                    //$("<form></form>").attr("action","/on/pushOn/"+data.id).attr("method","post").appendTo("body").submit().remove();

                    break;

                case 'pullOn' :

                    CoreUtil.sendPost("/on/pullOn/"+data.id, null, function (){
                        location.reload();
                    });
                    //$("<form></form>").attr("action","/on/pushOn/"+data.id).attr("method","post").appendTo("body").submit().remove();

                    break;
            }
        });

        //导出
        $('#export').on('click', function () {
            //原先分页limit
            var exportParams = {
                limit: 10000,
                key: $("#key").val()
            };
            CoreUtil.sendPost("/shop/listByPage", exportParams, function (res) {
                //初始化渲染数据
                if (res.data != null && res.data.records != null) {
                    table.exportFile(tableIns1.config.id, res.data.records, 'xls');
                }
            });
        });

        //删除
        var tipDialog = function (ids) {
            layer.open({
                content: "确定要删除么?",
                yes: function (index, layero) {
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    CoreUtil.sendDelete("/shop/delete", ids, function (res) {
                        layer.msg(res.msg, {time: 1000}, function () {
                            search();
                        });
                    });
                }
            });
        };

        //返回
        $("#btn_cancel").click(function () {
            $(".table_div").show();
            $(".operation").hide();
            return false;
        });

        //监听保存
        form.on('submit(submit)', function (data) {
            var arr=[];
            $.each(data.field,function (k,value) {
                var obj={
                    "key":k,
                    "value": value,
                    "type": 1
                };
                arr.push(obj);
            });
            console.info(data.field.id);
            if (data.field.id === undefined || data.field.id === null || data.field.id === "") {
                CoreUtil.sendPost("/shop/add", arr, function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    search();
                });
            } else {
                CoreUtil.sendPut("/shop/update", arr, function (res) {
                    $(".table_div").show();
                    $(".operation").hide();
                    search();
                });
            }
            return false;
        });
    });

    //执行查询
    function search() {
        //这里以搜索为例
        tableIns1.reload({
            where: { //设定异步数据接口的额外参数，任意设
                goodsName: $("#key").val()
            }
            , page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    };
</script>